using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Northwoods.Go;
using Northwoods.Go.Xml;
using Workflows.Components.Rules;

namespace Rules.Diagram
{
    public class TransformRuleNode : GoXmlTransformer
    {
        public TransformRuleNode()
        {
            this.TransformerType = typeof(RuleNode);
            this.ElementName = "node";
            this.IdAttributeUsedForSharedObjects = true;
        }

        public override void GenerateAttributes(Object obj)
        {
            base.GenerateAttributes(obj);
            RuleNode n = (RuleNode)obj;
            WriteAttrVal("kind", (int)n.Kind);
            WriteAttrVal("xy", n.Position);
            WriteAttrVal("size", n.Size);
            WriteAttrVal("label", n.Text);
            GoImage img = n.Image;
            if (img != null)
            {
                if (img.Name != null)
                    WriteAttrVal("imagename", img.Name);
                else if (img.Index >= 0)
                    WriteAttrVal("imageindex", img.Index);
            }
            if (n.Info != null) n.Info.GenerateAttributes(this);
        }

        public override Object Allocate()
        {
            RuleNode n = new RuleNode();
            RuleNodeType k = (RuleNodeType)Int32Attr("kind", (int)RuleNodeType.Action);
            n.Kind = k;
            n.ChangeNodeForKind(k);
            n.ChangeInfoForKind(k);
            return n;
        }

        public override void ConsumeAttributes(Object obj)
        {
            base.ConsumeAttributes(obj);
            RuleNode n = (RuleNode)obj;
            if (IsAttrPresent("label"))  // set text label first, before setting position/size?
                n.Text = StringAttr("label", n.Text);
            n.Position = PointFAttr("xy", new PointF(100, 100));
            n.Size = SizeFAttr("size", new SizeF(100, 40));
            if (IsAttrPresent("imagename"))
            {
                GoImage img = n.Image;
                if (img != null) img.Name = StringAttr("imagename", img.Name);
            }
            else if (IsAttrPresent("imageindex"))
            {
                GoImage img = n.Image;
                if (img != null) img.Index = Int32Attr("imageindex", img.Index);
            }
            if (n.Info != null) n.Info.ConsumeAttributes(this);
        }
    }
}
